package com.djl.sqtg.controller;

import com.alibaba.fastjson.JSONObject;
import com.djl.sqtg.entity.ColonelOrder;
import com.djl.sqtg.entity.GoodsSales;
import com.djl.sqtg.service.IColonelService;
import com.djl.sqtg.service.IGoodsService;
import com.djl.sqtg.service.IOrderService;
import com.djl.sqtg.service.IUserService;
import com.djl.sqtg.util.DateUtil;
import com.djl.sqtg.util.JsonResult;
import org.apache.catalina.LifecycleState;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("data")
public class DataController extends BaseController{
    @Autowired
    private IGoodsService goodsService;
    @Autowired
    private IUserService userService;
    @Autowired
    private IOrderService orderService;
    @Autowired
    private IColonelService colonelService;



    /*
     *获取后台第一个界面显示的统计数据
     *今日用户新增 userNewAdd             用户总数 userNum
     * 今日付款订单 todayOrders            团长总数 colonelNum
     * 今日付款金额 todayMoney             会员总数 memberNum
     * 待审核团长数 colonelAudit           近七天订单数 orderSevenNum
     * 订单总金额 totalMoney              商品总数 goodsNum
     * 团长总佣金 colonelMoney             近7天销售额（元）  sevenTurnover
     * */
    @GetMapping({"getData"})
    public JsonResult getData(){
        /*今日用户新增*/
        Integer userNewAdd = userService.userNewAdd(DateUtil.getToday());
        /*今日付款订单*/
        Integer todayOrders = orderService.todayOrders(DateUtil.getToday());
        /*今日付款金额*/
        Float todayMoney = orderService.todayMoney(DateUtil.getToday());
        if (todayMoney==null) todayMoney= Float.valueOf(0);
        /*待审核团长数*/
        Integer colonelAudit = colonelService.getAuditColonelNum();
        /*订单总金额*/
        Float totalMoney = orderService.totalMoney();
        /*团长总佣金*/
        Float colonelMoney = colonelService.colonelMoney();
        /*用户总数*/
        Integer userNum = userService.userNum();
        /*团长总数*/
        Integer colonelNum = colonelService.colonelNum();
        /*会员总数*/
        Integer memberNum = userService.memberNum();
        /*近七天订单数*/
        Integer orderSevenNum = orderService.orderSevenNum(DateUtil.getPastDate(6,DateUtil.getTodayDate()),DateUtil.getToday());
        /*商品总数*/
        Integer goodsNum =goodsService.goodsNum();
        /*近7天销售额（元）*/
        Float sevenTurnover = orderService.sevenTurnover(DateUtil.getPastDate(6,DateUtil.getTodayDate()),DateUtil.getToday());

        /*构建返回数据结构*/
        JSONObject data = new JSONObject();
        data.put("userNewAdd",userNewAdd);
        data.put("todayOrders",todayOrders);
        data.put("todayMoney",todayMoney);
        data.put("colonelAudit",colonelAudit);
        data.put("totalMoney",totalMoney);
        data.put("colonelMoney",colonelMoney);
        data.put("userNum",userNum);
        data.put("colonelNum",colonelNum);
        data.put("memberNum",memberNum);
        data.put("orderSevenNum",orderSevenNum);
        data.put("goodsNum",goodsNum);
        data.put("sevenTurnover",sevenTurnover);
        System.out.println("========前端首页统计数据=========");
        System.out.println(data);
        return  new JsonResult(OK,data);
    }





    /*
    * 用户数 userNum
    * 会员数 memberNum
    * 团长数 colonelNum
    * 订单数 ordersNum
    * 销售额 orderMoney
    * 商品销售数量排行 goods-sales
    * 商品销售金额排行 goods-money
    * 团长订单数量排行 colonel-orderNum
    * 团长订单金额排行 colonel-orderMoney
    * */
    @GetMapping({"getDataByDate/{date}"})
    public JsonResult getDataByDate(@PathVariable String date){
        System.out.println("根据相应时间获取相应数据*********************");
        System.out.println(date);
        Integer userNum=0;
        Integer memberNum=0;
        Integer colonelNum=0;
        Integer ordersNum=0;
        Float orderMoney=0.0f;
        List goodsSalesList=new ArrayList<>();
        List goodsMoneyList=new ArrayList<>();
        List colonelOrderNumList=new ArrayList<>();
        List colonelOrderMoneyList=new ArrayList<>();
        if (date.contains(",")){
            System.out.println("date包含逗号，说明是近三个月");
            String[] threeMonth=date.split(",");
            System.out.println(threeMonth[0]);
            System.out.println(threeMonth[1]);
            System.out.println(threeMonth[2]);
            for (int i=0;i<3;i++){
                userNum=userNum+userService.userNewAdd(threeMonth[i]);
                memberNum=memberNum+userService.memberNewAdd(threeMonth[i]);
                colonelNum=colonelNum+colonelService.colonelNewNum(threeMonth[i]);
                ordersNum=ordersNum+orderService.todayOrders(threeMonth[i]);
                Float orderMoneyItem=orderService.todayMoney(threeMonth[i]);
                if (orderMoneyItem==null) orderMoneyItem= Float.valueOf(0);
                orderMoney=orderMoney+orderMoneyItem;
            }
            orderMoney= Float.valueOf(String.format("%.2f", orderMoney));
            goodsSalesList = goodsService.getTMGoodsNumRank(threeMonth);
            goodsMoneyList = goodsService.getTMGoodsMoneyRank(threeMonth);
            colonelOrderNumList = colonelService.getTMColonelOrderNumRank(threeMonth);
            colonelOrderMoneyList = colonelService.getTMColonelOrderMoneyRank(threeMonth);
        }else if (date.contains("~")){
            System.out.println("date包含~，自定义时间段");
            String[]  dateArry  = date.split(" ~ ");
            String startDate = dateArry[0];
            String endDate = dateArry[1];
            userNum = userService.userNewAddTimePeriod(startDate,endDate);
            memberNum = userService.memberNewAddTimePeriod(startDate,endDate);
            colonelNum = colonelService.colonelNewNumTimePeriod(startDate,endDate);
            ordersNum = orderService.orderSevenNum(startDate,endDate);
            orderMoney = orderService.sevenTurnover(startDate,endDate);
            if (orderMoney==null) orderMoney= Float.valueOf(0);
            goodsSalesList = goodsService.getGoodsNumTimePeriod(startDate,endDate);
            goodsMoneyList = goodsService.getGoodsMoneyRankTimePeriod(startDate,endDate);
            colonelOrderNumList = colonelService.getColonelOrderNumRankTimePeriod(startDate,endDate);
            colonelOrderMoneyList = colonelService.getColonelOrderMoneyRankTimePeriod(startDate,endDate);
        }else {
            userNum = userService.userNewAdd(date);
            memberNum = userService.memberNewAdd(date);
            colonelNum = colonelService.colonelNewNum(date);
            ordersNum = orderService.todayOrders(date);
            orderMoney = orderService.todayMoney(date);
            if (orderMoney==null) orderMoney= Float.valueOf(0);
            goodsSalesList = goodsService.getTodayHotGoods(date);
            goodsMoneyList = goodsService.getGoodsMoneyRank(date);
            colonelOrderNumList = colonelService.getColonelOrderNumRank(date);
            colonelOrderMoneyList = colonelService.getColonelOrderMoneyRank(date);
        }

        JSONObject data = new JSONObject();
        data.put("userNum",userNum);
        data.put("memberNum",memberNum);
        data.put("colonelNum",colonelNum);
        data.put("ordersNum",ordersNum);
        data.put("orderMoney",orderMoney);
        data.put("goodsSalesList",goodsSalesList);
        data.put("goodsMoneyList",goodsMoneyList);
        data.put("colonelOrderNumList",colonelOrderNumList);
        data.put("colonelOrderMoneyList",colonelOrderMoneyList);
        return new JsonResult(OK,data);
    }


}
